package com.lzq.leetcode.hoot100;

import java.util.HashSet;
import java.util.Set;

public class s3 {
    public int lengthOfLongestSubstring(String s) {

        int max = 1;
        int len = s.length();
        int l = 0;
        int r = 1;
        char[] str = s.toCharArray();
        Set<Character> set = new HashSet<>();

        set.add(str[0]);
        while (r < len) {
            if (set.contains(str[r])) {
                // 包含了, 那么需要移动左指针
                while (str[l] != str[r]) {
                    set.remove(str[l]);
                    l++;
                }
                l++;
            } else {
                set.add(str[r]);
                max = Math.max(max, r - l + 1);
            }

            r++;
        }

        return max;
    }

    public static void main(String[] args) {
        System.out.println(new s3().lengthOfLongestSubstring("abcabcbb"));
    }
}
